package com.alongstudio.action.client.settings;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alongstudio.account.action.base.BaseAction;
import com.alongstudio.account.commons.exception.BusinessException;
import com.alongstudio.account.constants.IViewConstant;
import com.alongstudio.account.entity.ClientUser;
import com.alongstudio.account.entity.Merchant;
import com.alongstudio.account.service.IMerchantService;

@SuppressWarnings("serial")
@Component("MerchantAction")
@Scope("prototype")
public class MerchantAction extends BaseAction {

	@Autowired
	@Qualifier("MerchantService")
	private IMerchantService merchantService;
	private Integer id;
	private String merchantName;
	private String OPERATOR_RESULT;
	private List<Merchant> merchantList;
	private Merchant merchant;
	private String className = MerchantAction.class.getName();

	@Override
	public String executeAction() throws BusinessException {
		return SUCCESS;
	}

	public String getAllMerchant() throws BusinessException {
		try {
			merchantList = merchantService.findAllMerchant(getCurrentClientUserId());
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return SUCCESS;
	}

	public String newMerchant() throws BusinessException {
		boolean isOk = false;
//		try {
//			if (merchantName != null) {
//				Merchant merchant = new Merchant();
//				merchant.setMerchantName(merchantName);
//
//				ClientUser clientUser = new ClientUser();
//				clientUser.setId(getCurrentClientUserId());
//				merchant.setClientUser(clientUser);
//				merchant.setCreateTime(new Date());
//				int id = merchantService.createMerchant(merchant);
//				isOk = id > 0;
//			}
//		}
//		catch (BusinessException b) {
//			throw b;
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		OPERATOR_RESULT = isOk ? IViewConstant.SAVE_SUCCESS : IViewConstant.SAVE_FAILED;
		return SUCCESS;
	}

	public String updateMerchant() throws BusinessException {
		boolean isOk = false;
		try {
			if (merchant != null) {
				merchant.setUpdateTime(new Date());
				isOk = merchantService.updateMerchant(merchant);
			}
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		OPERATOR_RESULT = isOk ? IViewConstant.SAVE_SUCCESS : IViewConstant.SAVE_FAILED;
		return SUCCESS;
	}

	public String deleteMerchant() throws BusinessException {
		boolean isOk = false;
//		try {
//			if (id != null) {
//				isOk = merchantService.deleteMerchant(id);
//			}
//		}
//		catch (BusinessException b) {
//			throw b;
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		OPERATOR_RESULT = isOk ? IViewConstant.SAVE_SUCCESS : IViewConstant.SAVE_FAILED;
		return SUCCESS;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getMerchantName() {
		return merchantName;
	}

	public void setMerchantName(String merchantName) {
		this.merchantName = merchantName;
	}

	public String getOPERATOR_RESULT() {
		return OPERATOR_RESULT;
	}

	public void setOPERATOR_RESULT(String oPERATOR_RESULT) {
		OPERATOR_RESULT = oPERATOR_RESULT;
	}

	public List<Merchant> getMerchantList() {
		return merchantList;
	}

	public void setMerchantList(List<Merchant> merchantList) {
		this.merchantList = merchantList;
	}

	public Merchant getMerchant() {
		return merchant;
	}

	public void setMerchant(Merchant merchant) {
		this.merchant = merchant;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

}
